package ar.edu.itba.it.cg.grupo01.impl.shape;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.vecmath.Point2f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import ar.edu.itba.it.cg.grupo01.api.Intersection;
import ar.edu.itba.it.cg.grupo01.api.Ray;
import ar.edu.itba.it.cg.grupo01.api.shape.Shape;
import ar.edu.itba.it.cg.grupo01.impl.IntersectionFactory;

public class CompositeShape extends AbstractShape {

	private Set<Shape> members;

	public CompositeShape(Collection<? extends Shape> members,
			IntersectionFactory intersectionFactory) {
		super(intersectionFactory);
		this.members = new HashSet<Shape>(members);
	}

	@Override
	public Intersection<? extends Shape> intersects(Ray ray) {

		Set<Intersection<? extends Shape>> intersections = new HashSet<Intersection<? extends Shape>>();
		for (Shape shape : members) {
			intersections.add(shape.intersects(ray));
		}

		return intersectionFactory.createIntersection(intersections);
	}

	@Override
	public boolean containedInAABB(AABB box) {

		for (Shape shape : members) {
			if (shape.containedInAABB(box)) {
				return true;
			}
		}

		return false;
	}

	@Override
	public Point3f getMaxXYZ() {
		float xmax, ymax, zmax;

		xmax = ymax = zmax = Float.MIN_VALUE;

		for (Shape shape : members) {

			Point3f max = shape.getMaxXYZ();

			xmax = Math.max(xmax, max.x);
			ymax = Math.max(ymax, max.y);
			zmax = Math.max(zmax, max.z);
		}

		return new Point3f(xmax, ymax, zmax);
	}

	@Override
	public Point3f getMinXYZ() {
		float xmin, ymin, zmin;

		xmin = ymin = zmin = Float.MAX_VALUE;

		for (Shape shape : members) {

			Point3f min = shape.getMinXYZ();

			xmin = Math.min(xmin, min.x);
			ymin = Math.min(ymin, min.y);
			zmin = Math.min(zmin, min.z);

		}

		return new Point3f(xmin, ymin, zmin);
	}

	@Override
	public int getShapeCount() {
		return this.members.size();
	}
	
	public Set<Shape> getMembers() {
		return members;
	}

	@Override
	public Shape getTransformedShape() {

		return null;
	}

	@Override
	public Collection<Point3f> getVertices() {

		return null;
	}

	@Override
	public Vector3f getNormal(Point3f point) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Point2f getUVCoordinates(Point3f point) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Point3f getUVWCoordinates(Point3f point) {
		// TODO Auto-generated method stub
		return null;
	}

}
